package com.cloudy.cloudspider.search;

import com.google.gson.reflect.TypeToken;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Indexer {

    private IndexWriter writer;
    private Set<String> contentSet;

    public Indexer(String pre) {
        contentSet = new HashSet<>();
        String indexDir = SearcherHelp.getIndexDir(pre);
        //中文分词器
        SmartChineseAnalyzer analyzer = SearcherHelp.getAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);

        try {
            Directory dir = FSDirectory.open(Paths.get(indexDir));
            writer = new IndexWriter(dir, iwc);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取indexDir
     *
     * @throws Exception
     */
    public void index(Set<SearchNewsModel> models) {

        for (SearchNewsModel model : models) {
            if (contentSet.contains(model.getContext())) continue;
            contentSet.add(model.getContext());

            try {
                Document doc = new Document();
                doc.add(new IntField("time", model.getTime(), Field.Store.YES));
                doc.add(new TextField("title", model.getTitle(), Field.Store.YES));
//                doc.add(new TextField("titleImage", model.getTitleImage(), Field.Store.YES));
                doc.add(new TextField("context", model.getContext(), Field.Store.YES));
                doc.add(new TextField("shortContext", model.getShortContext(), Field.Store.YES));

                System.out.println("docs:"+doc.toString());

                writer.addDocument(doc);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public void close() throws IOException {
        contentSet.clear();
        writer.close();
    }

    public static void main(String[] args) throws Exception {
        String json = "[{\"author\":\"橘子娱乐\",\"context\":\"6510921751956816392薛之谦回应自己黑粉辱骂王杰：支持您维权到底\",\"time\":1515984903,\"title\":\"薛之谦回应自己黑粉辱骂王杰：支持您维权到底\",\"titleImage\":\"http://p3.pstatp.com/list/567300088c4b1d6e8b23\"},{\"author\":\"澎湃新闻\",\"context\":\"6511096055398924807\",\"time\":1515984902,\"title\":\"贵州茅台市值突破一万亿元大关，离中国银行只差一个涨停板\",\"titleImage\":\"http://p3.pstatp.com/list/56750001bc618cd14236\"},{\"author\":\"创业邦\",\"context\":\"6511072314275136013\",\"time\":1515984902,\"title\":\"爆料称腾讯春节推黄金红包；斗鱼：正在筹备上市\",\"titleImage\":\"http://p3.pstatp.com/list/593100057796af5dde48\"},{\"author\":\"专题\",\"context\":\"6509695627905991175\",\"time\":1515984902,\"title\":\"梅西暴力鸟破门，巴萨4球逆转；阿森纳爆冷负；大巴黎胜\",\"titleImage\":\"http://p9.pstatp.com/list/587a000336758b47349e\"},{\"author\":\"观海解局\",\"context\":\"6510908772095885827\",\"time\":1515984902,\"title\":\"国企老总为何说跟领导尿不到一个壶里？\",\"titleImage\":\"http://p3.pstatp.com/list/59350002aa2296c8a1a1\"},{\"author\":\"环球网\",\"context\":\"6511072595100566023\",\"time\":1515984902,\"title\":\"最坏结果出现：“桑吉”轮沉没了 泄漏的油该怎么办\",\"titleImage\":\"http://p3.pstatp.com/list/5672000e3b747504e88c\"},{\"author\":\"海外网\",\"context\":\"6511085612370493965\",\"time\":1515984902,\"title\":\"又一起！韩方扣押4艘中国渔船 称其“非法捕鱼”\",\"titleImage\":\"http://p3.pstatp.com/list/56730009c796482b1b27\"},{\"author\":\"海外网\",\"context\":\"6510833693945233934\",\"time\":1515984902,\"title\":\"“桑吉”轮剧烈燃烧后沉没 伊朗证实船上全员遇难\",\"titleImage\":\"http://p1.pstatp.com/list/566f0010a620b67756e9\"}]";
        List<SearchNewsModel> models = GsonHelper.parseJsonArray(json, new TypeToken<List<SearchNewsModel>>() {
        }.getType());

        new Indexer("test").index(new HashSet(models));
        System.out.println("Success Indexer");

    }

}